package com.zrc.module_find.viewmodel

import android.os.Bundle
import androidx.lifecycle.MutableLiveData
import com.zrc.module_find.NetResponse
import com.zrc.superframe_base.mvx.mvvm.BaseViewModel
import com.zrc.module_find.FindApi
import com.zrc.module_find.activity.defrecycleview.DemoResponse
import com.zrc.superframe_net.common.net.ApplySchedulers
import com.zrc.superframe_net.common.net.NetObserver
import com.zrc.superframe_net.common.net.Net
import com.zrc.superframe_net.common.net.NetCallBack
import com.zrc.superframe_net.interfac.NetBase

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Describe:
 */
class FindVM(var page: NetBase) : BaseViewModel() {

    override fun attach(savedInstanceState: Bundle?) {

    }


    val demoLiveData = MutableLiveData<SmartData<ArrayList<DemoResponse.OtherListBean>>>()
    fun getData(isRefrensh: Boolean=true,page_num: Int) {
        Net
            .getService(FindApi::class.java)
            .getData(20, page_num)
            .compose(ApplySchedulers())
            .subscribe(
                NetObserver(
                    page = page,
                    callback = object : NetCallBack<NetResponse<DemoResponse>>() {
                        override fun onSucceed(data: NetResponse<DemoResponse>, mothead: String) {
                            val dataRest=data.data.other_list?: arrayListOf()
                            when{
                                data.data.other_list==null-> demoLiveData.value = SmartData(code = -1,msg = data.msg,isRefrensh=isRefrensh,data=dataRest)
                                data.data.other_list.isNullOrEmpty()-> demoLiveData.value = SmartData(code = -2,msg = data.msg,isRefrensh=isRefrensh,data=dataRest)
                                else->demoLiveData.value = SmartData(code = 0,msg = data.msg,isRefrensh=isRefrensh,data=dataRest)
                            }
                        }

                        override fun onError(e: Throwable) {
                            demoLiveData.value = SmartData(code = -2,msg = e.message?:"",isRefrensh=isRefrensh,data=arrayListOf())
                        }
                    })
            )
    }


}